<template>
	<view class="container">
		<view class="uni-list">
			<view class="uni-list-cell uni-list-cell-pd">
				<view>邀请码</view>
				<view class="uni-list-cell-db"><input type="text" class="uni-input" v-model="inviteCode" placeholder="请输入" /></view>
			</view>
			<view class="uni-list-cell uni-list-cell-pd">
				<view>公司名称</view>
				<view class="uni-list-cell-db"><input type="text" class="uni-input" v-model="storeName" placeholder="请输入" /></view>
			</view>
			<view class="uni-list-cell uni-list-cell-pd">
				<view>公司所在地区</view>
				<view class="uni-list-cell-db" @click="show = !show">
					<u-picker :show="show" ref="uPicker" :loading="loading" :columns="columns" keyName="areaName"
						@change="changeHandler" itemHeight="88" @confirm="confirm"></u-picker>
						<view class="uni-input" type="text" style="padding: 15rpx 0;">{{citystr}}</view>
				</view>
				<view class="uni-navigate-right"></view>
			</view>
			<view class="uni-list-cell uni-list-cell-pd">
				<view>公司详细地址</view>
				<view class="uni-list-cell-db"><input type="text" v-model="address" class="uni-input" placeholder="请输入详细地址" /></view>
			</view>

		</view>
		<view class="title">经营范围</view>
		<view class="uni-list">
			<view class="uni-list-cell uni-list-cell-pd">
				<view>一级分类</view>
				<view class="uni-list-cell-db">
					<picker @change="firstCategoryChange" :value="firstIndex" :range="firstCategoryList" range-key="name">
						<view class="uni-input">{{firstCategoryList[firstIndex].name}}</view>
					</picker>
				</view>
				<view class="uni-navigate-right"></view>
			</view>
			<view class="uni-list-cell uni-list-cell-pd">
				<view>二级分类</view>
				<view class="uni-list-cell-db">
					<picker @change="secondCategoryChange" :value="secondIndex" :range="secondCategoryList" range-key="name">
						<view class="uni-input">{{secondCategoryList[secondIndex].name}}</view>
					</picker>
				</view>
				<view class="uni-navigate-right"></view>
			</view>
			<view class="uni-list-cell uni-list-cell-pd">
				<view>三级分类</view>
				<view class="uni-list-cell-db">
					<picker @change="thirdCategoryChange" :value="thirdIndex" :range="thirdCategoryList" range-key="name">
						<view class="uni-input">{{thirdCategoryList[thirdIndex].name}}</view>
					</picker>
				</view>
				<view class="uni-navigate-right"></view>
			</view>
			<view class="uni-list-cell uni-list-cell-pd">
				<view class="uni-list-cell-db">
					<view class="p">公司店铺照片</view>
					<u-upload :fileList="storeList" @afterRead="afterRead" @delete="deletePic" name="storeList" multiple
						:maxCount="maxCount" :width="218" :height="218" :accept="accept"></u-upload>
					<view class="p">授权证书</view>
					<u-upload :fileList="authList" @afterRead="afterRead" @delete="deletePic" name="authList" multiple
						:maxCount="maxCount" :width="218" :height="218" :accept="accept"></u-upload>
				</view>
			</view>
		</view>
		<view class="bottom">
			<u-button color="#06A972" text="确定" @click="applySave"></u-button>
		</view>

	</view>
</template>


<script>
	import { getCategoryList } from '@/api/cus.js';
	import { getAreaList } from '@/api/userAddr.js';
	export default {
		data() {
			return {
				show: false,
				loading: false,
				columns: [
					[],
					[],
					[]
				],
				provinceList: [],
				cityList: [],
				areaList: [],
				defaultProvince: 110000, //北京市
				defaultCity: 110100, //北京市
				citystr: '请选择所在地区',
				provinceCode: 0,
				cityCode: 0,
				districtCode: 0,
				firstCategoryList: [],
				secondCategoryList: [],
				thirdCategoryList: [],
				first: 0,
				second: 0,
				third: 0,
				address: '',
				storeName: '',
				inviteCode: '',
				storeList: [],
				authList: [],
				maxCount: 1,
				accept: 'image',
				firstIndex: 0,
				secondIndex: 0,
				thirdIndex: 0,
			}

		},
		onLoad() {
			/**
			 * 获取默认地址、分类
			 */
			let that = this;
			that.defaultAddress();
			that.defaultCategory(0);
		},
		methods: {
			/**
			 * 选择地址
			 * @param {Object} e
			 */
			changeHandler(e) {
				let that = this;
				const {
					columnIndex,
					index,
					// 微信小程序无法将picker实例传出来，只能通过ref操作
					picker = that.$refs.uPicker,
					value
				} = e
				if (columnIndex === 0) {
					that.loading = true
					// 获取市
					that.getAddress(value[columnIndex].areaId).then(res => {
						picker.setColumnValues(1, res)
			
						// 获取区
						that.getAddress(res[columnIndex].areaId).then(res => {
							picker.setColumnValues(2, res)
							that.loading = false
						})
					})
				}
				if (columnIndex === 1) {
					// 获取区
					that.getAddress(value[columnIndex].areaId).then(res => {
						picker.setColumnValues(2, res)
						that.loading = false
					})
				}
			},
			
			defaultAddress() {
				let that = this;
				// 获取所有省
				that.getAddress(1).then(res => {
					that.provinceList = res
			
					// 获取默认市
					that.getAddress(that.defaultProvince).then(res => {
						that.cityList = res
			
						// 获取默认区
						that.getAddress(that.defaultCity).then(res => {
							that.areaList = res
							that.columns = [
								[...that.provinceList],
								[...that.cityList],
								[...that.areaList]
							];
						});
					});
				});
			},
			
			getAddress(id) {
				return new Promise((resolve, reject) => {
					let that = this;
					getAreaList({
						parentId: id
					}).then(res => {
						resolve(res.value);
					}).catch(err => {
						that.$util.Tips({
							title: err.msg
						});
					})
				});
			},
			
			confirm(e) {
				let that = this;
				that.provinceCode = e.value[0].areaId;
				that.cityCode = e.value[1].areaId;
				that.districtCode = e.value[2].areaId;
				that.citystr = e.value[0].areaName + '/' + e.value[1].areaName + '/' + e.value[2].areaName;
				this.show = false;
			},
			
			
			/**
			 * 获取批发商服务分类
			 */
			getCategoryList(pid) {
				return new Promise((resolve, reject) => {
					let that = this;
					getCategoryList({
						pid: pid
					}).then(res => {
						resolve(res.value.list);
					}).catch(err => {
						that.$util.Tips({
							title: err.msg
						});
					})
				});
			},
			
			/**
			 * 默认批发商分类
			 */
			defaultCategory() {
				let that = this;
				that.getCategoryList(that.first).then(res => {
					that.firstCategoryList = res;
					that.first = that.firstCategoryList[that.first].cid
					
					that.getCategoryList(that.firstCategoryList[that.second].cid).then(res => {
						that.secondCategoryList = res;
						that.second = that.secondCategoryList[that.second].cid
						
						that.getCategoryList(that.secondCategoryList[that.third].cid).then(res => {
							that.thirdCategoryList = res;
							that.third = that.thirdCategoryList[that.third].cid
						});
					});
				});
			},
			
			/**
			 * 一级分类
			 */
			firstCategoryChange(e) {
				let that = this;
				that.first = that.firstCategoryList[e.detail.value].cid;
				that.firstIndex = e.detail.value;
				that.secondIndex = 0;
				that.thirdIndex = 0;
				that.getCategoryList(that.firstCategoryList[e.detail.value].cid).then(res => {
					that.secondCategoryList = res;
				});
			},
			
			/**
			 * 二级分类
			 */
			secondCategoryChange(e) {
				let that = this;
				that.second = that.secondCategoryList[e.detail.value].cid;
				that.secondIndex = e.detail.value;
				that.thirdIndex = 0;
				that.getCategoryList(that.secondCategoryList[e.detail.value].cid).then(res => {
					that.thirdCategoryList = res
				});
			},
			
			/**
			 * 三级分类
			 */
			thirdCategoryChange(e) {
				let that = this;
				that.thirdIndex = e.detail.value;
				that.third = that.thirdCategoryList.length && that.thirdCategoryList[e.detail.value].cid || '';
			},

			applySave() {
				let that = this;
				if (!that.inviteCode) return that.$util.Tips({
					title: '请输入邀请码'
				});
				
				if (!that.storeName) return that.$util.Tips({
					title: '请输入公司名称'
				});
				
				if (!that.address) return that.$util.Tips({
					title: '请输入详细地址'
				})
				
				if(!that.provinceCode) return that.$util.Tips({
					title: '请选择公司地址'
				})
				
				if(!that.first) return that.$util.Tips({
					title: '请选择一级分类'
				})
				
				if(!that.second) return that.$util.Tips({
					title: '请选择二级分类'
				})
				
				if(!that.third) return that.$util.Tips({
					title: '请选择三级分类'
				})
				
				if (that.authList.length < 1) return that.$util.Tips({
					title: '请上传授权证书'
				})
				
				if (that.storeList.length < 1) return that.$util.Tips({
					title: '请上店铺照片'
				})
				
				uni.showLoading({
					mask: true
				});
				that.$formdata.post('user/applySave',{
					"categoryList[0].cid": that.first,
					"categoryList[1].cid": that.second,
					"categoryList[2].cid": that.third,
					"storeName": that.storeName,
					"provinceCode": that.provinceCode,
					"cityCode": that.cityCode,
					"address": that.address,
					"districtCode": that.districtCode,
					"userInfoId": that.uid,
					"inviteCode": that.inviteCode,
				},{
					"authImage": that.authList[0].url,
					"storeImage": that.storeList[0].url
				}).then(res => {
					uni.hideLoading();
					that.$util.Tips({
						title: '申请成功',
					}, {
						tab: 3
					})
				}).catch(err => {
					that.$util.Tips({
						title: err
					});
				})
			},
			
			// 删除图片
			deletePic(event) {
				this[`${event.name}`].splice(event.index, 1)
			},
			// // 新增图片
			afterRead(event) {
				let that = this;
				// 当设置 multiple 为 true 时, file 为数组格式，否则为对象格式
				that.list = [].concat(event.file)
				// that.fileListLen = that[`${event.name}`].length
				that.list.map((item) => {
					that[`${event.name}`].push({
						...item,
						status: 'success',
						message: ''
					})
				})
			},
		}
	}
</script>

<style>
	.uni-list::before {
		display: none;
	}

	.uni-list::after {
		left: 30rpx;
	}

	.uni-list .uni-input {
		text-align: right;
	}

	.uni-list .p {
		padding-top: 30rpx;
	}

	.title {
		font-weight: bold;
		padding-left: 30rpx;
		padding-top: 30rpx;
	}

	.container .bottom {
		z-index: 99;
		box-sizing: border-box;
		padding: 60rpx;
		padding-bottom: calc(60rpx + constant(safe-area-inset-bottom));
		padding-bottom: calc(60rpx + env(safe-area-inset-bottom));
	}

	.uni-list .uni-list-cell {
		border-bottom: 1rpx solid #f8f8f8;
		margin: 0rpx 30rpx;
		padding: 22rpx 0rpx;
	}

	.uni-navigate-right:after {
		right: 0;
	}
	
	.u-upload__button .u-icon__icon {
		font-size: 28px !important;
	}
	
	.u-upload__deletable__icon .u-icon__icon {
		font-size: 20rpx !important;
		line-height: 20rpx !important;
	}
	
	.u-upload__success__icon .u-icon__icon {
		font-size: 24rpx !important;
		line-height: 24rpx !important;
	}
	
	.u-upload__wrap view:nth-child(3n) {
		margin-right: 0rpx;
	}
</style>